package com.huawei.appmarket.framework.startevents.roam;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import androidx.fragment.app.Fragment;
import com.huawei.appgallery.foundation.card.base.bean.AppInfoBean;
import com.huawei.appgallery.foundation.ui.framework.widget.ObservableScrollView;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.appmarket.framework.startevents.bean.DistStartupResponse;
import com.huawei.appmarket.service.exposure.bean.ExposureDetail;
import com.huawei.fastengine.fastview.startFastappEngine.bean.RpkInfo;
import huawei.widget.HwButton;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;
import o.azb;
import o.cfw;
import o.cip;
import o.BaseRequest;
import o.cvv;
import o.dgh;
import o.dgm;
import o.diy;
import o.diz;
import o.RoamDataManager;
import o.Logger;
import o.doo;
import o.ContentManager;
import o.dwe;
import o.dyf;
import o.dyg;
import o.epv;
import o.CollectCheck;
import o.evk;
import o.ewm.h;
import o.ewm.j;
import o.ewm.n;
import o.ll;

public class RoamFragment extends Fragment implements IServerCallBack, com.huawei.appgallery.foundation.ui.framework.widget.ObservableScrollView.a {
    private cip ʹ;
    private Timer ʻˋ = new Timer();
    private Context ʼ;
    private BroadcastReceiver ʼˋ = new BroadcastReceiver(this) {
        final /* synthetic */ RoamFragment ॱ;

        {
            this.ॱ = r1;
        }

        public void onReceive(Context context, Intent intent) {
            try {
                this.ॱ.ॱॱ = intent.getIntExtra("updatezjbbsize", this.ॱ.ॱॱ);
                this.ॱ.ॱॱ(this.ॱ.ॱॱ);
            } catch (IllegalStateException e) {
                Logger.e("RoamFragment", e.toString());
            }
        }
    };
    private View ʽ;
    private HwButton ˋ;
    private HwButton ˎ;
    private ObservableScrollView ˏ;
    private boolean ॱ = false;
    private int ॱॱ = 0;
    private LinearLayout ᐝ;
    private long ﹳ = 0;
    private dyf ﾞ = new dyf();
    private List<View> ﾟ = new ArrayList();

    public static class a implements Comparator, Serializable {
        public int compare(Object obj, Object obj2) {
            AppInfoBean appInfoBean = (AppInfoBean) obj;
            AppInfoBean appInfoBean2 = (AppInfoBean) obj2;
            if (appInfoBean.ॱˋ() < appInfoBean2.ॱˋ()) {
                return -1;
            }
            return appInfoBean.ॱˋ() == appInfoBean2.ॱˋ() ? 0 : 1;
        }
    }

    class e implements OnClickListener {
        final /* synthetic */ RoamFragment ˏ;

        private e(RoamFragment roamFragment) {
            this.ˏ = roamFragment;
        }

        public void onClick(View view) {
            this.ˏ.ˎ(view);
        }
    }

    private static int ˋ(int i) {
        Resources resources = ContentManager.getInstant().getContext().getResources();
        int ॱ = azb.ॱ(null, 0);
        int dimensionPixelSize = resources.getDimensionPixelSize(d.ᐨ) + epv.ˊ();
        if (i > 1) {
            return (ॱ - (i * dimensionPixelSize)) / (i - 1);
        }
        return 0;
    }

    public void ˏ(Bundle bundle) {
        super.ˏ(bundle);
        this.ʼ = ॱˋ().getApplicationContext();
        this.ॱ = false;
    }

    public View ˋ(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        super.ˏ(bundle);
        this.ʼ = ॱˋ().getApplicationContext();
        this.ʽ = layoutInflater.inflate(h.ـˏ, viewGroup, false);
        this.ˏ = (ObservableScrollView) this.ʽ.findViewById(j.ˉ);
        this.ˏ.setScrollStateListener(this);
        this.ᐝ = (LinearLayout) this.ʽ.findViewById(j.ʴᐝ);
        ʽˊ();
        ॱ();
        return this.ʽ;
    }

    private void ʾॱ() {
        View view = (LinearLayout) this.ʽ.findViewById(j.ˈʾ);
        view.setLayoutParams((LayoutParams) ॱ(view));
    }

    private void ʽᐝ() {
        ʾॱ();
        ˊʻ();
        ˋʽ();
        ʼᐝ();
    }

    private void ʼᐝ() {
        Intent intent = new Intent("com.huawei.appmarket.broadcast.updateroamnumchange");
        intent.putExtra("updatezjbbsize", diy.ˊ().ˋ().size());
        ll.ˏ(ContentManager.getInstant().getContext()).ˋ(intent);
    }

    private void ʽˊ() {
        this.ˋ = (HwButton) this.ʽ.findViewById(j.ˏߺ);
        this.ˎ = (HwButton) this.ʽ.findViewById(j.ꓸˎ);
        ॱॱ(this.ॱॱ);
        this.ˎ.setOnClickListener(new OnClickListener(this) {
            final /* synthetic */ RoamFragment ॱ;

            {
                this.ॱ = r1;
            }

            public void onClick(View view) {
                RoamDataManager.build();
                RoamDataManager.destory();
                diy.ˊ().ˏ();
                this.ॱ.ॱˋ().finish();
            }
        });
        ˉॱ();
    }

    protected void ॱ() {
        if (!RoamDataManager.build().isNotEmpty()) {
            ʿॱ();
        } else if (!this.ॱ) {
            ʽᐝ();
        }
    }

    private void ˉॱ() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.huawei.appmarket.broadcast.updateroamnumchange");
        ll.ˏ(this.ʼ).ˏ(this.ʼˋ, intentFilter);
    }

    private void ʿॱ() {
        ˊʼ();
        cfw.ˎ(cvv.ˎ(), this);
    }

    private void ॱॱ(int i) {
        this.ˋ.setText(ˎ(n.ߺˏ, Integer.valueOf(i)));
        if (i == 0) {
            this.ˋ.setClickable(false);
            this.ˋ.setAlpha(0.5f);
            return;
        }
        this.ˋ.setAlpha(1.0f);
        this.ˋ.setClickable(true);
        this.ˋ.setOnClickListener(new e());
    }

    public void ˎˎ() {
        super.ˎˎ();
        this.ॱ = true;
    }

    public void ᐝ() {
        if (this.ʼˋ != null) {
            ll.ˏ(this.ʼ).ˋ(this.ʼˋ);
        }
        super.ᐝ();
    }

    private void ˎ(View view) {
        if (!doo.ˊ(this.ʼ)) {
            evk.ˋ(this.ʼ, ᐝॱ().getString(n.ʹˊ), 0).ˋ();
        } else if (ˈॱ()) {
            dwe o_dwe = new dwe();
            ˋ(o_dwe);
            o_dwe.ˊ(ॱˋ(), view);
        } else {
            evk.ˋ(ॱˋ(), ॱ(n.ˊꜟ), 0).ˋ();
        }
    }

    private void ˋ(dwe o_dwe) {
        if (diy.ˊ().ˎ()) {
            for (Entry value : diy.ˊ().ˋ().entrySet()) {
                AppInfoBean appInfoBean = (AppInfoBean) value.getValue();
                ˎ(appInfoBean);
                o_dwe.ˏ(appInfoBean);
            }
        }
    }

    private void ˎ(AppInfoBean appInfoBean) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(RpkInfo.APPID, appInfoBean.ᐝ());
        linkedHashMap.put("type", String.valueOf(1));
        linkedHashMap.put("service_type", String.valueOf(dgm.ˎ(ॱˋ())));
        linkedHashMap.put("detailid", appInfoBean.ॱˊ());
        dgh.ˋ("card_installbtn_click", linkedHashMap);
    }

    public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
        if (!this.ॱ && (responseBean instanceof DistStartupResponse)) {
            ((DistStartupResponse) responseBean).ॱˎ();
        }
    }

    public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
        if (!this.ॱ) {
            List list = null;
            try {
                if (responseBean instanceof DistStartupResponse) {
                    list = ((DistStartupResponse) responseBean).ʿ();
                }
                if (CollectCheck.isEmpty(list)) {
                    this.ʹ.ˏ(responseBean.getResponseCode(), true);
                    return;
                }
                ˊʻ();
                RoamDataManager.build().ˎ(list);
                if (CollectCheck.isEmpty(list)) {
                    Logger.e("RoamFragment", "initContentView error,roamData is empty");
                } else {
                    ʽᐝ();
                }
            } catch (Exception e) {
                Logger.ˋ("RoamFragment", "notifyResult error!");
            }
        }
    }

    private boolean ˈॱ() {
        if (RoamDataManager.build().isNotEmpty()) {
            return true;
        }
        return false;
    }

    public void ˋᐝ() {
        super.ˋᐝ();
    }

    public void ˌ() {
        super.ˌ();
    }

    private ViewGroup.LayoutParams ॱ(View view) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams == null) {
            layoutParams = new LayoutParams(-1, -2);
        }
        int ॱ = azb.ॱ(view.getContext());
        if (2 == ॱˋ().getResources().getConfiguration().orientation) {
            layoutParams.width = ॱ;
            layoutParams.height = (int) (((double) ॱ) / 6.0d);
        } else {
            layoutParams.width = ॱ;
            layoutParams.height = (int) (((double) ॱ) / 3.0d);
        }
        return layoutParams;
    }

    private void ˊʼ() {
        if (this.ʹ == null) {
            this.ʹ = new cip();
            this.ʹ.ˎ(this.ʽ.findViewById(j.ˎₗ));
            this.ʹ.ˋ(new OnClickListener(this) {
                final /* synthetic */ RoamFragment ˏ;

                {
                    this.ˏ = r1;
                }

                public void onClick(View view) {
                    this.ˏ.ʿॱ();
                }
            });
        }
        this.ʹ.ॱ(0);
    }

    private void ˊʻ() {
        if (this.ʹ != null) {
            this.ʹ.ॱ(8);
            this.ʹ = null;
        }
    }

    private void ˋʽ() {
        List ˊ = RoamDataManager.build().getAppInfoBean();
        if (CollectCheck.isEmpty(ˊ)) {
            Logger.e("RoamFragment", "initAppListView error,roam Data is empty");
            return;
        }
        View view = null;
        int ˋ = ˋ(3);
        int size = ˊ.size() > 9 ? 9 : ˊ.size();
        diz o_diz = new diz();
        this.ﾟ.clear();
        int i = 0;
        while (i < size) {
            if (i % 3 == 0) {
                view = o_diz.ॱ(this.ʼ);
                this.ᐝ.addView(view);
            }
            View view2 = view;
            AppInfoBean appInfoBean = (AppInfoBean) ˊ.get(i);
            view = o_diz.ˊ(ॱˋ(), 3, i, appInfoBean, ˋ);
            if (view2 != null) {
                view.setTag(appInfoBean);
                this.ﾟ.add(view);
                view2.addView(view);
            }
            i++;
            view = view2;
        }
        this.ˏ.post(new Runnable(this) {
            final /* synthetic */ RoamFragment ˊ;

            {
                this.ˊ = r1;
            }

            public void run() {
                this.ˊ.ˋʻ();
            }
        });
    }

    public void ˋ(ObservableScrollView observableScrollView, int i) {
        if (i == 0) {
            ˋʻ();
        }
    }

    private void ˎ(List<AppInfoBean> list) {
        int ˎ = dgm.ˎ(ॱˋ());
        dyg ॱ = dyg.ॱ();
        for (AppInfoBean ॱˊ : list) {
            ExposureDetail ˎ2 = ExposureDetail.ˎ(ॱˊ.ॱˊ());
            if (ˎ2 != null) {
                ˎ2.ˊ("roam app");
                ॱ.ˎ(ˎ2, ˎ);
            }
        }
        this.ﾞ.ˊ(ॱ, ˎ);
    }

    private long ˊʽ() {
        return this.ﹳ;
    }

    private void ˋʻ() {
        this.ﹳ = System.currentTimeMillis();
        this.ʻˋ.schedule(new TimerTask(this) {
            final /* synthetic */ RoamFragment ॱ;

            {
                this.ॱ = r1;
            }

            public void run() {
                if (System.currentTimeMillis() - this.ॱ.ˊʽ() >= 1000) {
                    this.ॱ.ˋʼ();
                }
            }
        }, 1000);
    }

    private void ˋʼ() {
        List arrayList = new ArrayList();
        for (View view : this.ﾟ) {
            if (this.ﾞ.ˎ(view) && (view.getTag() instanceof AppInfoBean)) {
                arrayList.add((AppInfoBean) view.getTag());
            }
        }
        ˎ(arrayList);
    }
}
